﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class StudentsByFirstName
{
    static void Main()
    {
        List<Student> students = new List<Student>()
        {
            new Student("Pesho", "Peshov", "123106123", "+359 212312312", "pesho@abv.bg", new List<int>() {2,2,4}, 1, 1),
            new Student("Kamen", "Ivanov", "234111", "+3599343", "kamen@abv.bg", new List<int>() {3,2,2}, 2, 1),
            new Student("Vlado", "Kylev", "345111", "+3592 32312", "vlado@abv.bg", new List<int>() {3,3,4}, 2, 2),
            new Student("Mariq", "Gerogieva", "456106","+359 2 123","mariq@abv.bg", new List<int>() {5,4,6}, 2,2),
            new Student("Vladimira", "Georgieva", "12311105", "+3592232", "vladi@abv.bg", new List<int>() {4,5,6}, 1,1),
            new Student("Tihomir","Georgiev", "123106", "+359454", "tisho@abv.bg" ,new List<int>() {6,3,6}, 1,3),
        };

        //IEnumerable<Student> result =
        //    from student in students
        //    where student.GroupNumber == 2
        //    orderby student.FirstName
        //    select student;

        //foreach (Student std in result)
        //{
        //    Console.WriteLine(std.ToString());
        //    //Console.WriteLine(std.FirstName);
        //}

        //2
        //var res =
        //    students.Where(std => std.GroupNumber == 2).OrderBy(std => std.FirstName);
        //Console.WriteLine();
        //foreach (var item in res)
        //{
        //    Console.WriteLine(item.ToString());
        //}

        //3
        //var abvMails =
        //    from student in students
        //    where student.EMail.Contains("@abv")
        //    select student;
        //foreach (var item in abvMails)
        //{
        //    Console.WriteLine(item.ToString());
        //}

        //Regex telRegex = new Regex(@"\+359(\s)?2(\s)?[0-9]+");
        
        //4
        //var stdByTels =
        //    from student in students
        //    where telRegex.IsMatch(student.Tel)
        //    select student;

        //foreach (var item in stdByTels)
        //{
        //    Console.WriteLine(item.ToString());
        //}

        //5
        //var otlichnici =
        //    from student in students
        //    where (
        //        from mark in student.Marks
        //        where mark == 6
        //        select student).Count() >= 1
        //    select new { FullName = student.FirstName + " " + student.LastName, Marks = student.Marks };

        //foreach (var item in otlichnici)
        //{
        //    Console.WriteLine(item.FullName);
        //}

        //6
        //var dvoikari =
        //    students.Select(student => new { FullName = student.FirstName + " " + student.LastName, Marks = student.Marks }).Where(
        //        student => student.Marks.Count(mark => mark == 2) == 2);

        //foreach (var item in dvoikari)
        //{
        //    Console.WriteLine(item.FullName);
        //}

        //7
        //Regex graduateRegex = new Regex(@"(.|\s){4}06(.|\s)*");
        //var dveishestagrad =
        //    from student in students
        //    where graduateRegex.IsMatch(student.Fn)
        //    select student;
        //foreach (var item in dveishestagrad)
        //{
        //    Console.WriteLine(item.ToString());
        //}

        List<Group> groups = new List<Group>()
        {
            new Group(1,"Mathematics"),
            new Group(2,"Physics"),
            new Group(3,"Informatics"),
        };

        //8
        //var matematici =
        //    from student in students
        //    join gr in groups on student.GrNum equals gr.GroupNumber
        //    where gr.DepartmentName == "Mathematics"
        //    select student;

        //foreach (var item in matematici)
        //{
        //    Console.WriteLine(item);
        //}

        //9
        //string[] strings = 
        //{ 
        //    "asdasddas", 
        //    "asdasddasdasas",
        //    "asdasdddsdas",
        //    "asdasddas",
        //    "asdasddasdasdas",
        //    "asdasddaasdasdasdsas",
        //    "asdasddassss"
        //};

        //string res =
        //    (from str in strings
        //     orderby str.Length descending
        //     select str).First();
        //Console.WriteLine(res);     
        
        //10
        var res =
            from student in students
            join gr in groups on student.GrNum equals gr.GroupNumber
            group new { Std = student, Dep = gr.DepartmentName } by gr.DepartmentName;

        foreach (var item in res)
        {
            Console.WriteLine(item.Key);
            foreach (var asd in item)
            {
                Console.WriteLine(asd.Std.FirstName + " " + asd.Std.LastName);
            }
            Console.WriteLine();
        }

        //11
        //var res =
        //    students.Join(groups, (std => std.GrNum), (gr => gr.GroupNumber), (std, gr) => new { std, gr.DepartmentName}).
        //    GroupBy(gr => gr.DepartmentName);
        //foreach (var item in res)
        //{
        //    Console.WriteLine(item.Key + ":");
        //    foreach (var asd in item)
        //    {
        //        Console.WriteLine(asd.std.FirstName + " " + asd.std.LastName);
        //    }
        //    Console.WriteLine();
        //}
    }
}
